/**
 * @file TeacherDetailCard.vue
 * @description 教师详情卡片组件
 * @created 2024-06-25
 */

<template>
  <div class="teacher-detail-overlay" @click.self="closeDetail">
    <div class="teacher-detail-container">
      <div class="detail-header">
        <h2>教师详情</h2>
        <button class="close-btn" @click="closeDetail">&times;</button>
      </div>
      
      <div class="detail-body">
        <div class="teacher-avatar">
          <div class="avatar-placeholder">👨‍🏫</div>
          <h3>{{ teacher.teacher_name }}</h3>
          <p>{{ teacher.title }}</p>
        </div>
        
        <div class="detail-info">
          <div class="info-group">
            <h4>基本信息</h4>
            <div class="info-row">
              <span class="info-label">工号：</span>
              <span class="info-value">{{ teacher.teacher_code || '未设置' }}</span>
            </div>
            <div class="info-row">
              <span class="info-label">学院：</span>
              <span class="info-value">{{ teacher.college_name || '未设置' }}</span>
            </div>
            <div class="info-row">
              <span class="info-label">系部：</span>
              <span class="info-value">{{ teacher.department || '未分配' }}</span>
            </div>
          </div>
          
          <div class="info-group">
            <h4>教学信息</h4>
            <div class="info-row">
              <span class="info-label">课程数：</span>
              <span class="info-value">{{ teacher.course_count || 0 }}</span>
            </div>
            <div class="info-row">
              <span class="info-label">班级数：</span>
              <span class="info-value">{{ teacher.class_count || 0 }}</span>
            </div>
          </div>
          
          <div class="info-group">
            <h4>其他信息</h4>
            <div class="info-row">
              <span class="info-label">专业方向：</span>
              <span class="info-value">{{ teacher.specialty || '未设置' }}</span>
            </div>
            <div class="info-row">
              <span class="info-label">学历：</span>
              <span class="info-value">{{ teacher.education || '未设置' }}</span>
            </div>
            <div class="info-row">
              <span class="info-label">入职日期：</span>
              <span class="info-value">{{ formatDate(teacher.entry_date) }}</span>
            </div>
            <div class="info-row">
              <span class="info-label">教龄：</span>
              <span class="info-value">{{ calculateYears(teacher.entry_date) }}</span>
            </div>
          </div>
        </div>
      </div>
      
      <div class="detail-footer">
        <button class="edit-btn" @click="handleEdit">编辑信息</button>
      </div>
    </div>
  </div>
</template>

<script setup>
// 从props接收教师数据
const props = defineProps({
  teacher: {
    type: Object,
    required: true
  }
});

// 定义事件
const emit = defineEmits(['close', 'edit']);

// 关闭详情
const closeDetail = () => {
  emit('close');
};

// 编辑教师信息
const handleEdit = () => {
  emit('edit', props.teacher);
};

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return '未设置';
  
  try {
    const date = new Date(dateString);
    if (isNaN(date.getTime())) return '无效日期';
    
    return date.toLocaleDateString('zh-CN', {
      year: 'numeric',
      month: 'long',
      day: 'numeric'
    });
  } catch (error) {
    return '无效日期';
  }
};

// 计算工作年限
const calculateYears = (dateString) => {
  if (!dateString) return '未知';
  
  try {
    const entryDate = new Date(dateString);
    if (isNaN(entryDate.getTime())) return '未知';
    
    const now = new Date();
    let years = now.getFullYear() - entryDate.getFullYear();
    
    // 检查是否还没到今年的对应日期
    const isBefore = 
      now.getMonth() < entryDate.getMonth() || 
      (now.getMonth() === entryDate.getMonth() && now.getDate() < entryDate.getDate());
    
    if (isBefore) {
      years--;
    }
    
    return `${years}年`;
  } catch (error) {
    return '未知';
  }
};
</script>

<style scoped>
.teacher-detail-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.teacher-detail-container {
  background-color: white;
  border-radius: 8px;
  width: 600px;
  max-width: 95%;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #eaeaea;
}

.detail-header h2 {
  margin: 0;
  font-size: 20px;
  color: #333;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #999;
}

.close-btn:hover {
  color: #333;
}

.detail-body {
  padding: 20px;
  display: flex;
  flex-direction: column;
}

.teacher-avatar {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 20px;
}

.avatar-placeholder {
  font-size: 48px;
  width: 100px;
  height: 100px;
  background-color: #e6f7ff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 10px;
}

.teacher-avatar h3 {
  margin: 10px 0 5px;
  font-size: 20px;
  color: #333;
}

.teacher-avatar p {
  margin: 0;
  color: #666;
  font-size: 14px;
}

.detail-info {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.info-group {
  border: 1px solid #eaeaea;
  border-radius: 8px;
  padding: 15px;
}

.info-group h4 {
  margin: 0 0 15px;
  color: #1890ff;
  font-size: 16px;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 8px;
}

.info-row {
  margin-bottom: 8px;
  font-size: 14px;
  display: flex;
}

.info-label {
  color: #888;
  width: 100px;
  flex-shrink: 0;
}

.info-value {
  color: #333;
  font-weight: 500;
}

.detail-footer {
  padding: 15px 20px;
  border-top: 1px solid #eaeaea;
  text-align: center;
}

.edit-btn {
  padding: 8px 20px;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.edit-btn:hover {
  background-color: #40a9ff;
}
</style> 